<?php
namespace app\index\controller;

use app\index\controller\Base;
use app\index\service\IndexService;
use think\Request;
use think\Session;
use think\Db;

class Index extends Base
{
 public  function _initialize(){
	 	//$this->get_session();
		$this->heade();
	 }
    /**
     * 首页
    */
    public function index()
    {
        $indexService = new IndexService;
        if(Request::instance()->isGet()){
            $param = Request::instance()->param();
            //处理请求参数
            $arr = $indexService->getParam($param);
            // dump($arr);die;
            //获取用户
            $user = $indexService->getUser();
            //获取收藏
            $userCollect = $indexService->getUserCollect();
            // dump($userCollect);die;
            //获取商品的分类
            $goodsCates = $indexService->goodsCates(array('status'=>1));
            //获取商品
            $data = $indexService->allGoods($param);
            // dump($allGoods);die;
            $this->assign([
                'arr'         => $arr,
                'user'        => $user,
                'userCollect' => $userCollect,
                'goodsCates'  => $goodsCates,
                'data'        => $data,
            ]);
            return $this->fetch();
        }
    }

  	//用户退出
  	public function quit(){
  	     Session::clear();
  	     return $this->redirect('login/login');
  	}

   /* public function xiangqing()
    {
        return $this->fetch();
    }*/

    /**
     * 收藏商品
    */
    public function collecting()
    {
        if(Request::instance()->isPost()){
            $id = Request::instance()->post('id');
            $indexService = new IndexService;
            return json($indexService->collecting($id));
        }
    }

    /**
     * 我的收藏
    */
	  public function collect()
    {
    	$this->get_session();
        $indexService = new IndexService;
        if(Request::instance()->isGet()){
            $this->get_session();
            //获取商品的分类
            $data = $indexService->goodsCates();
            //处理请求参数
            $param = Request::instance()->param();
            $condition = $indexService->collectParam($param);
            //获取收藏的商品
            $collects = $indexService->collect($param);
            $this->assign([
                'data'      => $data,
                'condition' => $condition,
                'collects'  => $collects,
            ]);
            return $this->fetch();
        }else{
            $post = Request::instance()->post();
            return json($indexService->collect($post));
        }
    }

    public function getUserOrder()
    {
        $indexService = new IndexService;
        $indexService->getUserOrder();
    }

	//评论页面显示
	  public function review()
    {
        $id = input('id');
		$arr = Db::table('think_review')->alias('a')->join('think_user b','a.uid = b.id','RIGHT')->where('a.sp_id','EQ',$id)->field('b.mobile,a.content,a.time,a.reply')->select();
		foreach($arr as $key=>$v){
			$arr[$key]['mobile'] = substr($v['mobile'], 0, 3).'****'.substr($v['mobile'], 7);
		}
	    $this->assign('arr',$arr);
	    $this->assign('id',$id);
        return $this->fetch();
    }

	//添加评论
	 public function insert_review()
    {
      $user_session = Session::get('date');
	  $data['uid'] = $user_session['id'];
	  $arr = Request::instance()->post();
      //$arr = input();
	  $data['content'] = $arr['content'];
	  $data['sp_id'] = $arr['id'];
	  $repeat = Db::table('think_review')->where('sp_id','EQ',$data['sp_id'])->where('uid','EQ',$data['uid'])->find();
	  if( empty($repeat)){
	  $fid= Db::table('think_goods')->where('id','EQ',$arr['id'])->value('uid');
	  $data['f_id'] = $fid;
	  $data['time'] = time();
	  $res = Db::table('think_review')->insert($data);
	  if($res){
	  	$date['code']=1;
		$date['msg']='评论成功';
	  }else{
	  	$date['code']=0;
		$date['msg']='系统忙，请稍后再试';
	  }

    }else{
		$date['code']=0;
		$date['msg']='您已经评论过此商品';
	}
	 return $date;
   }

   /**
    * 测试获取订单
   */
    public function test()
    {
        $tiem = date('Y-m-d H:i:s',time()-1200);//获取当前时间20分钟之前的时间
        $str = urlencode($tiem);
        $url = 'http://api.tkurl.top/tbk_order?appkey=VYnPTEkn&start_time='.$str.'&span=1200&session=700001015086275a3a851e0ba3112f4b8ca06abff5047ab153a23a5d6b703881831d85d1580307426';
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,10);
        $dxycontent = curl_exec($ch);
        curl_close($ch);
        $data = json_decode ($dxycontent,true,512,JSON_BIGINT_AS_STRING);
        dump($data);die;
    }
}
